package com.reason.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.reason.search.dao.EsManagerMapper;
import com.reason.search.pojo.CustomerInfo;
import com.reason.search.service.EsManagerService;
import com.reason.user.feign.*;
import com.reason.user.feign.LifeStyleFeign;
import com.reason.user.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: zhangHeng
 * @date: 2020/6/30 10:25
 */
@Service
public class EsManagerServiceImpl implements EsManagerService {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private ConditionFeign conditionFeign;
    @Autowired
    private CustomerFeign customerFeign;
    @Autowired
    private EconomicsFeign economicsFeign;
    @Autowired
    private JobFeign jobFeign;
    @Autowired
    private LifeStyleFeign lifeStyleFeign;
    @Autowired
    private MarriageFeign marriageFeign;
    @Autowired
    private EsManagerMapper esManagerMapper;

    /**
     * 创建索引库
     */
    @Override
    public void createMappingAndIndex() {
        //创建索引
        elasticsearchRestTemplate.createIndex(CustomerInfo.class);
        //创建映射
        elasticsearchRestTemplate.putMapping(CustomerInfo.class);
    }

    /**
     * 导入全部数据
     */
    @Override
    public void importAll() {
        List<Customer> customerList =  customerFeign.findAll();
        if (customerList == null || customerList.size() <= 0) {
            throw new RuntimeException("当前没有查询到数据，无法导入索引库");
        }
        String s = JSON.toJSONString(customerList);
        List<CustomerInfo> customerInfos = JSON.parseArray(s, CustomerInfo.class);
        for (CustomerInfo customerInfo : customerInfos) {
            //获取经济实力数据
            Economics economics =economicsFeign.findById(customerInfo.getEconomics_id());
            //获取择偶表数据
            Condition condition = (Condition) conditionFeign.findById(customerInfo.getCondition_id());
            //获取工作学习数据
            Job  job = (Job) jobFeign.findById(customerInfo.getJob_id());
            //用户生活
            LifeStyle lifeStyle = (LifeStyle) lifeStyleFeign.findById(customerInfo.getLifestyle_id());
            //获取婚姻观念
            Marriage marriage = (Marriage) marriageFeign.findById(customerInfo.getMarriage_id());
            if(economics==null||condition==null||job==null||lifeStyle==null||marriage==null){
                throw new RuntimeException("当前没有查询到数据，无法导入索引库");
            }
            customerInfo.setEconomics(JSON.toJSONString(economics));
            customerInfo.setCondition(JSON.toJSONString(condition));
            customerInfo.setJob(JSON.toJSONString(job));
            customerInfo.setLifeStyle(JSON.toJSONString(lifeStyle));
            customerInfo.setMarriage(JSON.toJSONString(marriage));
        }
        esManagerMapper.saveAll(customerInfos);
    }
}
